﻿hpxAdminApp.controller('enterpriseBalanceController', function ($scope, $rootScope, $state, Upload, FILE_URL, PHOTO_FILE_URL, fileService, API_URL, NgTableParams, payService, orderService, constantsService) {
    var emptyEntity = {};
    var newEntity = angular.copy(emptyEntity, newEntity);

    $scope.filter = {
        'time1':'',
        'time2':''
    };

    $("#start_time").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        maxView: "decade",
        todayBtn: true,
        pickerPosition: "bottom-left",
        language:  'zh-CN'
    }).on("click",function(ev){
        $("#start_time").datetimepicker("setEndDate", $("#end_time").val());
    }).on('changeDate', function(e) {
        $scope.filter.time1 = $("#start_time").val();
    });

    $("#end_time").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        maxView: "decade",
        todayBtn: true,
        pickerPosition: "bottom-left",
        language:  'zh-CN'
    }).on("click", function (ev) {
        $("#end_time").datetimepicker("setStartDate", $("#start_time").val());
    }).on('changeDate', function(e) {
        $scope.filter.time2= $("#end_time").val();
    });

    //获取所有正在进行中的订单
    $scope.tableParams = new NgTableParams({ sorting: { 'id': 'asc' } }, {
        getData: function (params) {
            return payService.getEnterpriseBalance(params, $scope.filter).then(function (data) {
                $scope.first = $scope.getFirst(params);
                return data;
            });
        }
    });
    // 刷新
    $scope.reflash = function () {
        $scope.tableParams.reload();
    }


    $scope.show = function (data) {
        $scope.model = angular.copy(data);      //弹出详细窗口
        $('#modal-show').modal('show');
        showEnterpriseBalanceInfo($scope.model.id); 
    };



    //显示企业充值详情
    function showEnterpriseBalanceInfo(id) {
        $scope.tableParams2 = new NgTableParams({ sorting: { 'id': 'desc' } }, {
            getData: function (params) {
                return payService.getEnterpriseBalanceWithId(id,params, $scope.filter).then(function (data) {
                    $scope.first = $scope.getFirst(params);
                    return data;
                });
            }
        });

    }
    // 刷新
    $scope.reflash2 = function () {
        showEnterpriseBalanceInfo($scope.model.id);
    }

    $scope.add = function (data) {
        $scope.model = angular.copy(data);      //弹出详细窗口
        $('#modal-add').modal('show');
    };
    $scope.addBalance = function () {
        payService.updateEnterpriseBalanceWithId($scope.model.id,$scope.model).then(function (result) {
                alert("充值成功");
                $('#modal-add').modal('hide');
        })
    }

    $scope.madeBillPhoto = function (is_made_bill) {
        if (is_made_bill==1) {
            //已开票，点击可看到图片

        }else{
            //未开票
            uploadFiles();
        }
    }

    //获取文件的url
    $scope.getFileURL = function (id) {
        if (id != null) {
            return FILE_URL + '/file' + id;
        }
    }
    //上传文件
    $scope.uploadFiles = function (files, errFiles, successFunc,item) {
        $scope.uploading = true;
        if (errFiles.length > 0) {
            confirm('有文件不符合要求，无法上传！');
        }
        
        angular.forEach(files, function (file) {
            
                var fd = new FormData();
                fd.append("file", file);
                var xhr;
                if (window.XMLHttpRequest) { // Mozilla, Safari, IE7+ ...
                    xhr = new XMLHttpRequest();
                } else if (window.ActiveXObject) { // IE 6 and older
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xhr.addEventListener('load', function () {
                    if (this.status == 200 || this.status == 304) {
                        var data2 = JSON.parse(xhr.responseText);
                       item.bill_photo_url = data2.data.file_path;
                        makeBill(item.id, data2.data.id);
                        
                    }

                }, false);
                xhr.open('POST', PHOTO_FILE_URL + '/file', true);
                xhr.send(fd);
        });

       
    };

    $scope.zoomPic = function (url) {
        $scope.model.bill_photo_path = url;
        $('#modal-zoom').modal('show');
    };

    //设置开票照片
    makeBill = function (id,file_id) {
        param = { "bill_photo_id": file_id }
        payService.updateEnterpriseBalanceMakeBill(id, param).then(function (result) {
            alert("上传成功");
            $scope.tableParams2.reload();
        })
    };


    //设置开票照片为上传的文件
    $scope.setMakeBillID = function (response) {
        $timeout(function () {
            //alert("ddd");
            $scope.model.is_made_bill_id = response.data.data.id;
            $scope.model.is_made_bill_id_rul = response.data.data.file_path;
        })
    };

    var onLoadHandler = function (event) {
        if (this.status == 200 || this.status == 304) {
            var data = JSON.parse(event.response);
            
        }
    };

    function dataURLtoBlob(dataurl) {
        var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new Blob([u8arr], { type: mime });
    }

});